import base, image, text, data, inputs

class template(base.tag):
    def toHTML(self):
        return self.childHTML()

class profile(template):
    def __init__(self, name="", img_path="",
                 email="", phone="", duties="",
                 dpmt="", edu="", interests="",
                 address="", img_dims=[100, 100],
                 childs=[]):
        template.__init__(self, [])
        self.name = name
        self.image = image.image(img_path, img_dims)
        self.email = email
        self.phone = phone
        self.duties = duties
        self.dpmt = dpmt
        self.edu = edu
        self.interests = interests
        self.address = address

        table = data.table()
        table.addChild(data.tr().fromList([self.image,
                                           self.getContactInfo()]))
        tr = data.tr()
        td = data.td([ data.table().fromList(
            [[self.edu],
             [self.interests]])
                       ])
        td.setSpan(2)
        tr.addChild(td)
        table.addChild(tr)

        table.setColumnWidth(0, self.image.dims[1])
        self.addChild(table)

    def getContactInfo(self):
        return contact_info(name=self.name,
                            email=self.email,
                            phone=self.phone,
                            address=self.address)


class profile_page(template):
    def __init__(self, profiles=[]):
        pass

class caption(template):
    def __init__(self, img, txt):
        template.__init__(self, [])
        self.addChild(data.table().fromList(
            [[img],
             [txt]]))

class group_caption(image.array):
    def __init__(self, srcs=[], txts=[],
                 img_dims=[100, 100], grp_dims=[2, 3]):
        self.names = txts
        image.array.__init__(self, srcs, img_dims, grp_dims)

    def makeList(self):
        l = image.array.makeList(self)
        nl = []
        for r in l:
            nsl = []
            for ele in r:
                nsl.append(caption(ele, self.names.pop()))
            nl.append(nsl)
        return nl
    
    def toHTML(self):
        return image.array.toHTML(self)

class contact_info(template):
    def __init__(self, name="", email="",
                 phone="", address=""):
        template.__init__(self, [])
        self.addChild(data.table().fromList(
                [[name],
                 [email],
                 [phone],
                 [address]]))


class basic_page(template):
    def __init__(self, head_con, body_con):
        template.__init__(self, [])
        b = base.body(body_con)
        h = base.head(head_con)
        ht = base.html([h, b])
        self.addChild(base.root([ht]))


class report_form(template):
    def __init__(self, title, f_lst,
                 handler, init="", table=data.table()):
        #
        # Handler should be Javascript of the form func(%s)
        #   %s will be replaced with an array of all
        #   the field object's ids
        #
        sub = inputs.button([base.string("Submit Report")])
        hstr = "{%s " % init
        for field in f_lst:
            if f_lst[field][1]:
                hstr +="q%s:'%s', " % (field, f_lst[field][1].getID())
        hstr = hstr[:len(hstr)-2] + " }"
        sub.addTagOpt("onclick", handler % hstr)
        
        field_lst = [f_lst[f] for f in f_lst]
        template.__init__(self, [
                data.table().fromList( [
                        [ text.h3(title)],
                        [ table.fromList(field_lst) ],
                        [ sub ]])])
